package nc.bs.gl.excel.bp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import nc.bs.framework.common.RuntimeEnv;

public class PropBp {
	
	private static String path = null;

	public Map<String, Properties> readAll() {
		Map<String, Properties> outMap = new HashMap<String, Properties>();
		
		File file = new File(getPath());
		
		File[] files = file.listFiles();
		for(File f1 : files){
			if(f1.getName().endsWith(".properties")){
				String[] names = f1.getName().split("\\.");
				Properties prop = getProp(f1);
				outMap.put(names[0], prop);
			}
		}
		
		return outMap;
	}
	
	public static Properties getProp(File file){
		
		Properties prop = new Properties();
		FileInputStream fis = null;
		InputStreamReader inr = null;
		try {
			fis = new FileInputStream(file);
			inr = new InputStreamReader(fis, "UTF-8");
			prop.load(inr);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (inr != null)
				try {
					inr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			
			if (fis != null)
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		
		return prop;
	}
	
	private static String getPath(){
		
		if(path == null){
			String nchome = RuntimeEnv.getInstance().getNCHome();
			path = nchome + "/resources/gl/excel/";
		}
		
		return path;
	}
	
}
